﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForDataReader.cs
 * Module Version:  1.0.0 (2014-3-14)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension collection instance's method.  It supplies 
 * useful extension methods, such as ToString(), FetchString() and so on.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace UCF.Core
{
    public static class ExMethodForDataReader
    {
        public static string MergeText(this IDataReader dr, int start = 0, int end = 0, string splitor = "|", bool isPair = true, Func<int,object, object> funcField = null)
        {
            if (end == 0 || end >= dr.FieldCount) end = dr.FieldCount;
            if (start > end) return string.Empty;
            if (dr.GetName(start).Contains("详情"))
            {
                return dr.GetValue(start).ToString();
            }
            StringBuilder sb = new StringBuilder();
            string tpl = isPair ? "{0}={1}{2}" : "{1}{2}";
            while (start < end)
            {
                sb.AppendFormat(tpl, dr.GetName(start), funcField == null ? dr[start] : funcField(start,dr[start]), splitor);
                start++;
            }
            if (sb.Length == 0) return string.Empty;
            return sb.Remove(sb.Length - splitor.Length, splitor.Length).ToString();
        }
    }
}
